/**
 * ixSlider 테마존
 * @package	{ixSlider}
 * @version 20130731 (jslee)
*/

if ( ixSlider ) throw new Error('[ixError] "ixSlider"가 이미 존재하여 충돌이 발생!');

var ixSlider = ( function ( $container) {
	var _$slider = $container,
		_$bannerList = _$slider.find( '.slides-container' ).children(),	
		_$nextBtn = _$slider.find( '.prev-next a.next'),
		_$prevBtn = _$slider.find( '.prev-next a.prev');

	var	_tlBanner = _$bannerList.length,
		_crtBanner = 0,
		_oldBanner = 0,
		_width = _$bannerList.eq(1).width();

	init();
	
	function init () {
		if ( _tlBanner != 0 ) {
			arrangeBanner();
			addBtnEvent();
		}		
	}
	
	function arrangeBanner () {
		for ( var i = 0 ; i < _tlBanner ; ++i ){
			_$bannerList.eq( i ).css( 'left' , ( _width * i )+ 'px');	
		}
	}

	function addBtnEvent () {		
		_$nextBtn.on( 'click' , nextBtnMouseHandler);
		_$prevBtn.on( 'click' , prevBtnMouseHandler);
		_$bannerList.find( 'div.apps-thumb-list-p > ul li a' ).on( 'focusin', thumbListFocusHandler );
	}
	
	function thumbListFocusHandler (e) {		
		var page = $( this ).parent().parent().parent().parent().index();					
		if ( _crtBanner < page ){
			moveQuickStep( 1 );				
		}else if ( _crtBanner > page ){
			moveQuickStep( -1 );
		}		
	}

	function nextBtnMouseHandler (e) {
		moveStep( 1 );
		return false;
	}
	
	function prevBtnMouseHandler (e) {		
		moveStep( -1 );
		return false;
	}	

	function moveStep ( step ){		
		_oldBanner = _crtBanner;
		_crtBanner += step;		
		if ( _crtBanner == _tlBanner ){
			_crtBanner = 0;
		}else if ( _crtBanner < 0 ) {
			_crtBanner = _tlBanner - 1;
		}
		var $oldImg = _$bannerList.eq( _oldBanner ),
			$crtImg = _$bannerList.eq( _crtBanner );
		if ( step > 0 ){
			$oldImg.animate( {left: ( _width * -1 )+'px'} , {queue :false} );			
			$crtImg.css( 'left', _width + 'px' );
		}else{
			$oldImg.animate( {left: _width + 'px'} , {queue :false} );
			$crtImg.css( 'left', ( _width * -1 )+'px' );
		}
		$crtImg.animate( {left: '0px'} , {queue :false} );		
	}

	function moveQuickStep ( step ){		
		_oldBanner = _crtBanner;
		_crtBanner += step;		
		if ( _crtBanner == _tlBanner ){
			_crtBanner = 0;
		}else if ( _crtBanner < 0 ) {
			_crtBanner = _tlBanner - 1;
		}
		var $oldImg = _$bannerList.eq( _oldBanner ),
			$crtImg = _$bannerList.eq( _crtBanner );
		if ( step > 0 ){
			$oldImg.css( {'left': ( _width * -1 )+'px'} )						
		}else{
			$oldImg.css( {left: _width + 'px'} );			
		}
		$crtImg.css( 'left', 0 + 'px' )
	}
});

/**
 * ixBanner 빅배너 ( 프리미엄초이스 하단)
 * @package	{ixBanner}
 * @version 20130802 (jslee)
*/

if ( ixBanner ) throw new Error('[ixError] "ixBanner"가 이미 존재하여 충돌이 발생!');
var ixBanner = ( function ( $container) {
	var _$slider = $container,
		_$banner = _$slider.find( '#gmnextslide' )
		_$bannerList = _$banner.children(),	
		_$nextBtn = _$slider.find( '#gmnextnext'),
		_$prevBtn = _$slider.find( '#gmnextprev');

	var	_tlBanner = _$bannerList.length,		
		_viewIdx = 0,
		_viewNum = 3,
		_viewArr = [],		
		_width = 238;

	init();
		
	function init () {
		if ( _tlBanner != 0){
			checkBtn();
			arrangeBanner();
			addBtnEvent();			
		}
	}
	
	function arrangeBanner () {
		for ( var i = 0 ; i < _tlBanner ; ++i ){
			_$bannerList.eq( i ).css( 'left' , ( _width * i )+ 'px');
			_viewArr.push(i);
		}
	}
	
	function checkBtn () {		
		if ( _tlBanner < _viewNum ){
			_$prevBtn.hide();
			_$nextBtn.hide();
		}else{
			if ( _viewIdx > 0 ){ 
				_$prevBtn.show();
			}else{
				_$prevBtn.hide();
			}
			if ( _viewIdx  + _viewNum == _tlBanner ){
				_$nextBtn.hide(); 
			}else{
				_$nextBtn.show();
			}
		}
	}

	function initListArr () {
		_viewArr = [];
		for ( var i = 0 ; i <  _tlBanner ; i++ ){
			_viewArr.push( i );
		}
		_viewIdx = _viewArr[0];
		checkBtn();
	}

	function addBtnEvent () {
		//_$bannerList.find( 'a' ).on( 'focusin', mouseHandler );		 2013.09.24 jslee delete
		_$nextBtn.on( 'click' , nextBtnMouseHandler);
		_$prevBtn.on( 'click' , prevBtnMouseHandler);	
	}
	/* 2013.09.24 jslee delete
	function mouseHandler (e) {		
		var idx =  $( this ).parent().index();	
		if ( idx == 0 ){
			initListArr();
			moveStep();
		}else{
			if ( idx > _viewIdx ){
				nextBtnMouseHandler(e);
			}else{
				prevBtnMouseHandler(e);
			}
		}
	}
*/
	function nextBtnMouseHandler (e) {
		if ( _viewIdx + _viewNum  < _tlBanner  ){
			swapList( 1 );
		}
		return false;
	}
	
	function prevBtnMouseHandler (e) {		
		if ( _viewIdx != 0){
			swapList( -1 );
		}
		return false;
	}	
		
	function swapList ( step ) {
		if ( step > 0 ) {
			_viewArr.push( _viewArr.shift() );
		}else {
			_viewArr.unshift( _viewArr.pop() );
		}		
		_viewIdx = _viewArr[0];
		moveStep()
	}	

	function moveStep () {				
		checkBtn();
		_$banner.animate( {'margin-left': ( -_viewIdx * _width ) + 'px'} , 'fast' );
	}
});


/**
 * ixFeaturedApps 프리미엄 초이스
 * @package	{ixFeaturedApps}
 * @version 20130805 (jslee)
*/
if ( ixFeaturedApps ) throw new Error('[ixError] "ixFeaturedApps"가 이미 존재하여 충돌이 발생!');
var ixFeaturedApps = ( function ( $container) {	
	var _$container = $container,
		_$slider = $container.find( 'div.slides-container' ),
		_$bannerList =_$slider.children(),
		_$pagingList = $container.find( 'ul.pagination' ).children(),		
		_$autoPlayBtn = $container.find( '.pause-play a.play' ),
		_$autoPauseBtn = $container.find( '.pause-play a.pause' ),
		_$thumbBtnList = _$bannerList.find( 'div.apps-info a' ),			
		_$eventBanenr = _$slider.find( '.event-list' );

	var	_tlBanner = _$bannerList.length,
		_crtBanner = 0,
		_oldBanner = 0,
		_width = 660,
		_isAutoRolling = true,
		_intervalId = 0,
		_intervalTime = 5000;
		
	$container.find( '.loading' ).fadeOut();
	init();

	function init () {				
		if ( _tlBanner == 0 ) return;
		arrangeBanner();
		_$container.find( 'ul.apps-block li div.vimg' ).wrapInner( "<div class='move'></div>" );
		addBtnEvent();
	}
	
	function arrangeBanner () {
		for ( var i = 0 ; i < _tlBanner ; ++i ){
			_$bannerList.eq(i).css( { 'left' : ( _width * i + 0 ) + 'px'  }).fadeIn();
		}		
	}
	
	function addBtnEvent () {
		
		_$bannerList.find( 'div.apps-info a' ).on( 'focusin focusout' , thumbListFocusHandler );		
		_$bannerList.find( 'ul.apps-block li' ).on( 'mouseenter mouseleave' , thumbListMouseHandler );		
				
		if ( _tlBanner > 1 ){
			_$container.find( '.prev-next a.next' ).on( 'click' , nextBtnMouseHandler );
			_$container.find( '.prev-next a.next' ).on( 'mouseenter mouseleave' , thumbListMouseHandler );
			_$container.find( '.prev-next a.prev' ).on( 'click' , prevBtnMouseHandler );
			_$container.find( '.prev-next a.prev' ).on( 'mouseenter mouseleave' , thumbListMouseHandler );
			_$autoPlayBtn.on( 'click' , autoPlayPauseBtnHandler );
			_$autoPauseBtn.on( 'click' , autoPlayPauseBtnHandler );

			if ( _$eventBanenr ){
				_$eventBanenr.find( 'a' ).on( 'focusin focusout' , eventFocusHandler );
				_$eventBanenr.find( 'a' ).on( 'mouseenter mouseleave' , thumbListMouseHandler );
			}
			_$pagingList.on( 'click' , pagingClickHandler );
			_$pagingList.on( 'mouseenter mouseleave focusin focusout' , thumbListMouseHandler ); // 2013.09.17 add (페이징 버튼에서 focusin/out일 경우 자동롤링 방지)

			autoPlay();

		}else{
			_$container.find( '.prev-next a.next' ).hide();
			_$container.find( '.prev-next a.prev' ).hide();
			_$autoPlayBtn.hide();
			_$autoPauseBtn.hide();
			_$pagingList.hide(); //2013.09.26 jslee add (1페이지일 경우 삭제)
		}

		$("ul.apps-block li").hover(function(){
			$(this).find(".move").animate({left:'-98px'},{queue:false,duration:300});
		}, function(){
			$(this).find(".move").animate({left:'0px'},{queue:false,duration:300});
		});			
	}

	function autoPlay () {
		clearInterval(_intervalId);
		_intervalId = setInterval( nextBanenr , _intervalTime );
	}

	function nextBanenr () {	
		moveStep(1);
		move(1);
	}

	function autoPlayPauseBtnHandler (e) {
		_isAutoRolling = !_isAutoRolling;
		_$autoPlayBtn.toggle();
		_$autoPauseBtn.toggle();

		if ( _isAutoRolling ){
			if ( _tlBanner > 1 ) autoPlay();
		}else{
			clearInterval(_intervalId);
		}
	}

	function pagingClickHandler (e) {		
		var idx = $( this ).index(),
			gap = idx - _crtBanner
		
		moveStep ( gap );
		move( gap );
		return false;
	}

	function eventFocusHandler (e) {		
		if ( e.type == 'focusin' ){
			var tlEvent = _$eventBanenr.length,
				idx = $(this).parent().index();

			arrangeBanner();
			if ( _isAutoRolling ){
				clearInterval(_intervalId);
			}

			_oldBanner = _crtBanner
			_crtBanner = idx;
			//console.log ( 'eventFocusHandler : ' ,_oldBanner, _crtBanner);
			var $oldImg = _$bannerList.eq( _oldBanner ),
				$crtImg = _$bannerList.eq( _crtBanner );

			for ( var i = 0 ; i < _tlBanner ; ++i ){
				_$bannerList.eq(i).css( 'left' , ( _width * i + 660 ) + 'px') ;
			}

			$oldImg.css( {left: ( _width * 1 )+'px'} );
			$crtImg.css( 'left', '0px' );		

			changePagging();
		}else{
			if ( _isAutoRolling ){
				if ( _tlBanner > 1 ) autoPlay();
			}
		}
	}

	function thumbListFocusHandler (e) {
		e.preventDefault();	
		if ( e.type == 'focusin' ){
			if ( _isAutoRolling ){
				clearInterval(_intervalId);
			}
			$(this).parent().parent().css( 'left' , '-98px' );

			var page = $( this ).parent().parent().parent().parent().parent().parent().index();		
			if ( _crtBanner < page ){
				moveStep( 1 );	
				moveQuick(1);	
			}else if ( _crtBanner > page ){
				moveStep( -1 );				
				moveQuick(-1);
			}
					
		}else if ( e.type == 'focusout' ){
			$(this).parent().parent().css( 'left' , '0px' );
			if ( _isAutoRolling ){
				if ( _tlBanner > 1 ) autoPlay();
			}
		}
	}
	
	function thumbListMouseHandler (e) {
		if ( e.type == 'mouseenter' ||  e.type == 'focusin' ) {
			if ( _isAutoRolling ){
				clearInterval(_intervalId);
			}
		}else if ( e.type == 'mouseleave' || e.type == 'focusout' ) {
			if ( _isAutoRolling ){
				if ( _tlBanner > 1 ) autoPlay();
			}
		}
	}
	function nextBtnMouseHandler (e) {
		moveStep( 1 );
		move( 1 );
		return false;
	}
	
	function prevBtnMouseHandler (e) {
		moveStep( -1 );
		move( -1 );
		return false;
	}

	function moveStep ( step ){		
		_oldBanner = _crtBanner;
		_crtBanner += step;
		if ( _crtBanner == _tlBanner ){
			_crtBanner = 0;
		}else if ( _crtBanner < 0 ) {
			_crtBanner = _tlBanner - 1;
		}		
	}

	function moveQuick ( step ) {
		var $oldImg = _$bannerList.eq( _oldBanner ),
			$crtImg = _$bannerList.eq( _crtBanner );
	
		if ( step > 0 ){
			$crtImg.css( 'left', '0px' );
			$oldImg.css( {left: ( _width * -1 )+'px'} );
		}else{
			$oldImg.css( {left: ( _width * 1 )+'px'} );
			$crtImg.css( 'left', '0px' );
		}
		changePagging();
	}

	function move ( step ) {		
		var $oldImg = _$bannerList.eq( _oldBanner ),
			$crtImg = _$bannerList.eq( _crtBanner );
		if ( step > 0 ){
			$oldImg.animate( {left: ( _width * -1 )+'px'} , {queue :false} );						
			$crtImg.css( 'left', _width + 'px' ).animate( {left: '0px'}, {queue :false});
		}else{
			$oldImg.animate( {left: _width + 'px'} , {queue :false} );
			$crtImg.css( 'left', ( _width * -1 )+'px' ).animate( {left: '0px'}, {queue :false});
		}
		changePagging();
	}

	function changePagging () {
		_$pagingList.eq( _oldBanner ).removeClass( 'current' );
		_$pagingList.eq( _crtBanner ).addClass( 'current' );
	}
});


/**
 * ixSpecialPromotion 스페셜프로모션
 * @package	{ixSpecialPromotion}
 * @version 20131015 (kwangwoonchoi)
*/
if ( ixSpecialPromotion ) throw new Error('[ixError] "ixSpecialPromotion"가 이미 존재하여 충돌이 발생!');
var ixSpecialPromotion = ( function ( $container) {	
	/* 2013.08.27 jslee add */					
	var _$slider = $container,
		_$banner =  $container.find( '.slides-container' ),
		_$bannerList = _$banner.children(),	
		_$nextBtn = _$slider.find( '.prev-next a.next'),
		_$prevBtn = _$slider.find( '.prev-next a.prev');

	var	_tlBanner = _$bannerList.length,							
		_viewIdx = 0,
		_viewArr = [],	
		_width = _$bannerList.eq(1).width();

	init();
	
	//hiake 
	function init () {
		if ( _tlBanner != 0 ) {								
			arrangeBanner();
			addBtnEvent();
			checkBtn();
			initListArr();
			checkPage();//2013.10.01 jslee add (선택된 이벤트로 페이지 이동 추가)
		}		
	}

	//2013.10.01 jslee add (선택된 이벤트로 페이지 이동 추가)
	function checkPage (){
		var page = _$bannerList.find( 'a.on').parent().parent().parent().index();
		for ( var i = 0; i < page ; i++){
			swapList( 1 , false );
		}
		checkBtn();
		_$banner.animate( {'margin-left': ( -page * _width ) + 'px'} , 'fast' );						
	}

		
	function arrangeBanner () {
		for ( var i = 0 ; i < _tlBanner ; ++i ){
			_$bannerList.eq( i ).css( 'left' , ( _width * i )+ 'px');									
		}
	}

	function addBtnEvent () {		
		_$nextBtn.on( 'click' , nextBtnMouseHandler);
		_$prevBtn.on( 'click' , prevBtnMouseHandler);							
	}

	function checkBtn () {		
		if ( _tlBanner == 1 ){
			_$prevBtn.hide();
			_$nextBtn.hide();
		}else{
			if ( _viewIdx > 0 ){ 
				_$prevBtn.show();
			}else{
				_$prevBtn.hide();
			}
			if ( _viewIdx  == _tlBanner - 1 ){
				_$nextBtn.hide(); 
			}else{
				_$nextBtn.show();
			}
		}
	}
	
	function initListArr () {
		_viewArr = [];
		for ( var i = 0 ; i <  _tlBanner ; i++ ){
			_viewArr.push( i );
		}
		_viewIdx = _viewArr[0];
		checkBtn();
	}

	function addBtnEvent () {							
		_$nextBtn.on( 'click' , nextBtnMouseHandler);
		_$prevBtn.on( 'click' , prevBtnMouseHandler);	
	}
	
	//hiake
	function nextBtnMouseHandler (e) {
		if ( _viewIdx  < _tlBanner  ){
			swapList( 1 , true ); // 2013.10.01 jslee modify ( type 추가 )
		}
		return false; // 2013.10.01 jslee add
	}
	
	function prevBtnMouseHandler (e) {		
		if ( _viewIdx != 0){
			swapList( -1 , true ); // 2013.10.01 jslee modify ( type 추가 )
		}
		return false; // 2013.10.01 jslee add
	}	
	
	// 2013.10.01 jslee modify ( type 추가 )
	function swapList ( step , type ) {
		if ( step > 0 ) {
			_viewArr.push( _viewArr.shift() );
		}else {
			_viewArr.unshift( _viewArr.pop() );
		}		
		_viewIdx = _viewArr[0];

		if ( type == true){							
			moveStep();
		}
	}	

	function moveStep () {				
		checkBtn();
		_$banner.animate( {'margin-left': ( -_viewIdx * _width ) + 'px'} , 'fast' );
	}
});
